package com.example.crawler.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.example.crawler.model.HighSchool;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;


/**
 * @Author hjq
 *
 * @Date 03/01/2019 17:29
 *
 * @Description //TODO
 */

public class Spider {

    /**
     * 获取dom文件
     * @param url
     * @return
     */
    private static Document loadDocumentData(String url) {
        // 创建文档对象
        Document doc = null;
        // 根据url将某页信息转换成文档
        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 返回文档
        return doc;
    }




    /**
     * 获取高校的url地址
     * @param url  http://chengdu.xuexiaodaquan.com/gaozhong/
     * @return
     */
    private static List<String> getListSchool(String url){
        //获取成都高校的dom文件
        Document document = loadDocumentData(url);
        //获取所有高校的链接list
        Elements schoolList = document.select(".list dl dd a");
        String allSchool = schoolList.toString().replace("<a href=\"","");
        //初始化我们想要的数据 最终只有URL 地址的list
        List<String> list = Arrays.asList(allSchool.split("</a>"));
        ArrayList<String> urlList = new ArrayList<>();
        //初始化URL 地址
        list.forEach(dd->{
            int theLast = dd.indexOf(".html\"")+5;
            urlList.add(dd.substring(0,theLast));
        });
        System.out.println(urlList.size());
        return urlList;
    }


    /**
     * 获取最后一页
     * @param url 用户地址
     * @return
     */
    private static Integer getLastPage(String url,String type){
        //获取成都高校的dom文件
        Document document = loadDocumentData(url);
        //获取最后一页的数据 查看总共有多少页
        Elements lastPageStr = document.getElementsByClass("last");
        if (null == lastPageStr || lastPageStr.size() == 0){
            return 0;
        }
        int startNum = lastPageStr.toString().indexOf("/"+type+"/pn")+type.length()+4;
        int endNum = lastPageStr.toString().indexOf(".html");
        //初始化高校总的页数
        int lastPage = Integer.parseInt(lastPageStr.toString().substring(startNum,endNum));
        return lastPage;
    }
    /**
     * 初始化校区服务
     * @param url
     * @return
     */
    private static HighSchool initHighSchool(String url){
        List<String> tagList = getTags(url);
        List<String> infoList = getInfo(url);
        HighSchool highSchool = new HighSchool();
        for (int i =0;i<tagList.size();i++){
            highSchool.setName(infoList.get(infoList.size()-1));
            if ("地址".equals(tagList.get(i))){
                highSchool.setAddress(infoList.get(i));
                continue;
            }
            if ("\n电话".equals(tagList.get(i))){
                highSchool.setPhoneNum(infoList.get(i));
                continue;
            }
            if ("\n邮编".equals(tagList.get(i))){
                highSchool.setEmail(infoList.get(i));
                continue;
            }
            if ("\n网址".equals(tagList.get(i))){
                highSchool.setUrl(infoList.get(i));
                continue;
            }
            if ("\n附近站点".equals(tagList.get(i))){
                highSchool.setSite(infoList.get(i));
                continue;
            }
            if ("\n公交路线".equals(tagList.get(i))){
                highSchool.setBusRoutes(infoList.get(i));
                continue;
            }
            if ("\n附近地标".equals(tagList.get(i))){
                highSchool.setLandmark(infoList.get(i));
                continue;
            }
            if ("\n学校级别".equals(tagList.get(i))){
                highSchool.setSchoolLavel(infoList.get(i));
            }
        }

        return highSchool;
    }

    @Test
    public static List<HighSchool> getHighSchool(String url,String type){
        //获取最后的一页
        int lastPage =getLastPage(url,type);
        List<HighSchool> list = new ArrayList<>();
        List<String> stringList = getListSchool(url);
        stringList.forEach( infoUrl -> {
            list.add(initHighSchool(infoUrl));
        });

        for (int i = 2;i <= lastPage; i++){
            String pageUrl = url+"/pn"+i+".html";
            List<String> strings = getListSchool(pageUrl);
            strings.forEach( infoUrl -> {
                System.out.println(infoUrl);
                list.add(initHighSchool(infoUrl));
            });
        }
        return list;
    }



    /**
     * 获取所有的标签信息
     * @param url url地址
     * @return
     */
    private static List<String> getTags(String url){
        //获取详情页的document
        Document document = loadDocumentData(url);
        //获取school的ul 信息
        Elements schoolUl = document.select(".detail-xx ul li span");
        //获取所有标签的信息 list
        String tagName = schoolUl.toString().replace("<span>","").replace("</span>","");
        List<String> tagList = Arrays.asList(tagName.split("："));
        return tagList;
    }

    /**
     * 获取相关信息
     * @param url
     * @return
     */
    private static List<String> getInfo(String url){
        //获取详情页的document
        Document document = loadDocumentData(url);
        //获取所有标签的信息 list
        String tagName = document.select(".detail-xx ul li i").toString().replace("<i>","");
        List<String> infoList = Arrays.asList(tagName.split("</i>"));
        String schoolName = document
                .select(".list-title h1 p").toString()
                .replace("<p>","")
                .replace("</p>","");
        List<String> newInfoList =new ArrayList<>();
        newInfoList.addAll(infoList);
        newInfoList.add(schoolName);
        return newInfoList;
    }

    @Test
    public void test1(){
        //获取详情页的document
        Document document = loadDocumentData("http://chengdu.xuexiaodaquan.com/xuexiao/468454.html");
        String schoolName = document
                .select(".list-title h1 p").toString()
                .replace("<p>","")
                .replace("</p>","");
    }


}
